<template>
    <div>

        <van-nav-bar
                title="不凡"
                left-text="返回"
                @click-left="onClickLeft"
                  fixed
                placeholder 
                />
            <van-address-edit
                    :area-list="areaList"
                    show-postal
                    show-delete
                    show-set-default
                    show-search-result
                    :search-result="searchResult"
                    :area-columns-placeholder="['请选择', '请选择', '请选择']"
                    @save="onSave($event)"
                    @delete="onDelete($event)"
                    @change-detail="onChangeDetail"
                    :address-info="addressinfo"
                    />
    </div>
</template>

<script>
import { Area } from 'vant';
import areaList from "../../../../public/areaList.js"
import {saveAction , addressinfo ,deleteAction} from "../../../api/my/index.js"
import { Toast } from 'vant';
    export default {
        data() {
            return {
                areaList,
                searchResult:[],
                name:"",
                addressinfo:{}

            }
        },
        methods:{
            onClickLeft(){
                    this.$router.go(-1)
            },
            onSave(event){
                var openId = localStorage.getItem("openId")
                   
                    console.log(event);
                     console.log(event.tel);
                    saveAction({
                       address:event.province+event.city+event.county,
                       addressId:"",
                       checked:event.isDefault,
                       detailadress:event.addressDetail,
                       openId:openId,
                       telNumber:event.tel,
                       userName:event.name

                    }).then(res=>{
                        console.log(res);
                      if(res.data==true){
                           Toast('保存成功');
                           this.$router.push({path:"/address"})
                      }else {
                           Toast('添加失败');
                      }
                    })
            },
            onDelete(event){
                console.log(event);
                    deleteAction({
                        id:event.id
                    }).then(res=>{
                       if(res.data==true){
                            Toast("删除成功");
                            this.$router.push({path:"/address"})
                       }else {
                            Toast("删除失败");
                       }
                    })
            },
            onChangeDetail(){

            }

        },
        created(){
            
             var idinfo = this.$route.query.id
            if(idinfo!=undefined){
                    addressinfo({
                        id:idinfo
                    }).then(res=>{
                        // console.log(res.data.id,"看看");
                        var obj = {
                            id:res.data.id,
                            name:res.data.name,
                            tel:res.data.mobile,
                            addressDetail:res.data.address_detail,
                        }
                        this.addressinfo = obj
                        
                    })
            }
        }
    }
</script>

<style lang="scss" scoped>

</style>